package com.smartaccounting.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DatabaseUtil {
    // 修改这里的密码为您的实际MySQL密码
    private static final String URL = "jdbc:mysql://localhost:3306/smart_accounting?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "123456";

    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            throw new ExceptionInInitializerError("数据库驱动加载失败");
        }
    }

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(URL, USERNAME, PASSWORD);
    }

    // 测试连接
    public static void main(String[] args) {
        try (Connection conn = getConnection()) {
            System.out.println("✅ 数据库连接成功!");
        } catch (SQLException e) {
            System.out.println("❌ 数据库连接失败: " + e.getMessage());
            System.out.println("请检查：");
            System.out.println("1. MySQL服务是否启动");
            System.out.println("2. 数据库名是否正确：smart_accounting");
            System.out.println("3. 用户名密码是否正确");
            System.out.println("4. 数据库是否已创建（执行前面的SQL脚本）");
        }
    }
}